﻿using System;
using System.IO;
using System.Text;

namespace JyCommonKit.FileKit
{
    /// <summary>
    /// 文件操作帮助类
    /// </summary>
    public static class FileHelper
    {
        // 拷贝文件
        public static bool CopyTo(string metaPath, string targetPath)
        {
            return false;
        }

        // 剪切文件
        public static bool CutTo(string metaPath, string targetPath)
        {
            return false;
        }


        /// <summary>
        /// 存储text文件
        /// </summary>
        /// <param name="path"> 文件路径 </param>
        /// <param name="name"> 文件名 </param>
        /// <param name="conent"> 需要存的内容 </param>
        /// <returns> 是否存储成功 </returns>
        public static bool SaveTextFile(string path, string name, string conent)
        {            
            if(string.IsNullOrEmpty(path) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(conent))
            {
                Console.WriteLine("存储路径或内容为空！");
                return false;
            }

            StreamWriter sw;
            var newPath = $"{path}/{name}";
            var fileInfo = new FileInfo(newPath);
            if (fileInfo.Exists)
            {
                sw = fileInfo.CreateText();
            }
            else
            {
                sw = fileInfo.AppendText();
            }

            sw.Write(conent);
            sw.Close();
            sw.Dispose();

            return true;
        }


        /// <summary>
        /// 读取 text 文件
        /// </summary>
        /// <param name="path"> 路径 </param>
        /// <param name="name"> 名字 </param>
        /// <returns> 读取的内容 </returns>
        public static string ReadTextFile(string path, string name)
        {
            if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(name))
            {
                Console.WriteLine("文件路径为空！");
                return "";
            }

            var newPath = $"{path}/{name}";
            var fileInfo = new FileInfo(newPath);
            if (!fileInfo.Exists)
            {
                Console.WriteLine("文件路径为空！");
                return "";
            }

            StreamReader sr = fileInfo.OpenText();
            StringBuilder sb = new StringBuilder();
            while (!sr.EndOfStream)
            {
                sb.Append(sr.ReadLine());
            }

            sr.Close();
            sr.Dispose();

            return sb.ToString();
        }

    }
}
